package com.example.myapplication.view_type;

import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

/**
 * 用于管理ViewPager中的页面
 */
public class ContentAdapter extends PagerAdapter {

    private List<View> views;

    public ContentAdapter(List<View> views) {
        this.views = views;
    }

    // 返回页面的数量，即views列表的大小
    @Override
    public int getCount() {
        return views.size();
    }

    // 检查给定的View是否与对象匹配
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    // 获取并添加指定位置的View到容器中
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        return view;
    }

    // 从容器中销毁指定位置的 View
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

}